package xian.woniuxy.be.web;

import org.apache.shiro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import xian.woniuxy.be.entity.Contactperson;
import xian.woniuxy.be.entity.Contactrecord;
import xian.woniuxy.be.entity.ContactrecordExample;
import xian.woniuxy.be.entity.ResultVO;
import xian.woniuxy.be.service.IContactrecordService;

import java.util.List;

/**
 * @author Gong
 * @time 2022/01/09 00:46:12
 */
@RestController
public class ContactrecordController {

    @Autowired
    private IContactrecordService contactrecordService;

    @PostMapping("contactrecord/save")
    public ResultVO save(@RequestBody Contactrecord contactrecord){

        if (contactrecord.getCusId() == null){
            return new ResultVO(501, "客户不能为空", null);
        }
        if (contactrecord.getReDate() == null){
            return new ResultVO(502, "活动时间不能为空", null);
        }
        if (contactrecord.getReAddr() == null){
            return new ResultVO(503, "活动地点不能为空", null);
        }
        if (contactrecord.getReSummary() == null){
            return new ResultVO(504, "活动概要不能为空", null);
        }

        contactrecordService.save(contactrecord);
        return ResultVO.success("操作成功",null);
    }

    @PostMapping("contactrecord/delete")
    public ResultVO delete(@RequestBody String reId){
        if (!StringUtils.hasText(reId)){
            throw new RuntimeException("来往记录ID为空");
        }
        contactrecordService.delete(Integer.valueOf(reId));
        return ResultVO.success("删除成功");
    }

    @PostMapping("contactrecord/update")
    public ResultVO update(@RequestBody Contactrecord contactrecord){

        if (contactrecord.getCusId() == null){
            return new ResultVO(501, "客户不能为空", null);
        }
        if (contactrecord.getReDate() == null){
            return new ResultVO(502, "活动时间不能为空", null);
        }
        if (contactrecord.getReAddr() == null){
            return new ResultVO(503, "活动地点不能为空", null);
        }
        if (contactrecord.getReSummary() == null){
            return new ResultVO(504, "活动概要不能为空", null);
        }

        contactrecordService.update(contactrecord);
        return ResultVO.success("修改成功");
    }

    @PostMapping("contactrecord/findAll")
    public ResultVO findAll(){
        List<Contactrecord> list = contactrecordService.findAll();
        return ResultVO.success(list);
    }

    @PostMapping("contactrecord/findOne")
    public ResultVO findOne(@RequestBody String cusId){
        if (!StringUtils.hasText(cusId)){
            throw new RuntimeException("客户ID为空");
        }
        List<Contactrecord> list = contactrecordService.findOne(Integer.valueOf(cusId));
        return ResultVO.success(list);
    }
}
